package org.example.seckilljava.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.example.seckilljava.entity.UserQuota;

@Mapper
public interface UserQuotaMapper extends BaseMapper<UserQuota> {
    @Select("select * from t_user_quota where user_id = #{userID} and goods_id = #{goodsID}")
    UserQuota getUserGoodsUserQuota(@Param("userID") long userID, @Param("goodsID") long goodsID);

    @Update("update t_user_quota set killed_num = killed_num + #{num} " +
            "where user_id = #{userID} and goods_id = #{goodsID} and killed_num + #{num} <= num")
    Integer incrKilledNum(@Param("userID") Long userID, @Param("goodsID") Long goodsID, @Param("num") Integer num);

    @Insert("insert into t_user_quota(user_id, goods_id, num, killed_num) " +
            "values (#{userQuota.userId}, #{userQuota.goodsId}, #{userQuota.num}, #{userQuota.killedNum})")
    void save(@Param("userQuota") UserQuota userQuota);
}